package service;

import java.util.List;

import javax.swing.JList;
import javax.swing.table.DefaultTableModel;

import model.Course;
import model.SC;
import model.Student;
import dao.SCDao;
import dao.StuDao;

public class SCService {
	SCDao scDao = new SCDao();
	//查询所有课程，并放入list中
	public void setAllCourseToList(java.awt.List list){
		List <Course> cours = scDao.findAllCourse();
		for(Course course : cours){
			String str = course.getId() + "-" + course.getCname() + "-" + course.getPeriod();
			list.add(str);
		}
	}
	
	//查询选了某节课的所有学生，并放入tm表格中
	public void setStuByCidToTable(int id , DefaultTableModel tm){
		List <Student> stus = scDao.findStuByCid(id);
		tm.setRowCount(0);
		for(Student stu : stus){
			String [] str = {stu.getId()+"", stu.getName(), stu.getAge()+"",stu.getSex()};
			tm.addRow(str);
		}
	}

	//查找所有学生，放入list中
	public void findAllStuToList(java.awt.List list){
		StuDao stuDao = new StuDao();
		List <Student> stus = stuDao.findAll();
		list.removeAll();
		for(Student stu : stus){
			String str = stu.getId()+"-"+stu.getName();
			list.add(str);
		}
	}

	//查找某个学生选课的信息,被选课程并放入tm1,没选的课程放入tm2
	public void setSelCourByStuIdToTable(int id , DefaultTableModel tm1 ,DefaultTableModel tm2){
		List <Course> selCours = scDao.findSelCourByStuId(id);
		CourseService courseService = new CourseService();
		List <Course> cours = courseService.findAllCourse();
		tm1.setRowCount(0);
		for(Course cour : selCours){
			String [] str = {cour.getId()+"", cour.getCname(), cour.getPeriod()+""};
			tm1.addRow(str);
			boolean flag = cours.remove(cour);
		}
		tm2.setRowCount(0);
		for(Course cour : cours){
			String [] str = {cour.getId()+"", cour.getCname(), cour.getPeriod()+""};
			tm2.addRow(str);
		}
	}
	
	public boolean SelCour(int stuId , int courId){
		return scDao.SelCour(stuId, courId);
	}
	
	public boolean DelSelCour(int stuId , int courId){
		return scDao.DelSelCour(stuId, courId);
	}
}
